package com.ruoyi.member.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author huang
 * @Date 2023/8/16 15:01
 * @PackageName: com.ruoyi.member.job
 * @ClassName: MyMapEnable
 * @Description:
 * @Version 1.0
 */
@Component("myMapEnable")
public class MyMapEnable {

    private static Scheduler scheduler;
    private static TriggerKey key;

    static {
        try {
            scheduler = new StdSchedulerFactory().getScheduler();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void enable() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(MyMapJob.class).withIdentity("enable").build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trriger1").startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()).build();
        MyMapEnable.key = trigger.getKey();
        MyMapEnable.scheduler.start();
        MyMapEnable.scheduler.scheduleJob(jobDetail,trigger);
    }

    public static void stop() {
        try {
            MyMapEnable.scheduler.unscheduleJob(key);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

}
